/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * PersistentLogin entity data model.  This model is used to store persistent logins "remember me".
 * @author Adam
 * @version $Id: PersistentLogin.java 24 2009-05-15 04:38:19Z a.ruggles $
 * 
 * Created on Apr 17, 2009 at 6:56:04 PM 
 */
@javax.persistence.Entity @Table(name = "sapien_persistent_logins")
@NamedQueries({
	@NamedQuery(name = "persistentLogin.listUser",
			query = "FROM com.google.code.sapien.model.PersistentLogin pl WHERE pl.user = :user"),
	@NamedQuery(name = "persistentLogin.getUserToken",
			query = "SELECT pl FROM com.google.code.sapien.model.PersistentLogin pl "
				+ "WHERE pl.user = :user and pl.token = :token")
})
public class PersistentLogin implements Serializable {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 8712035820065868203L;

	/**
     * Unique identifier.
     */
    private Long id;

    /**
     * The hash of the persistent login token.
     */
    private String token;

    /**
     * The persisted user.
     */
    private User user;

    /**
     * Default Constructor.
     */
    public PersistentLogin() { }

    /**
     * Constructs a persistent login object with a user and token.
     * @param user The user.
     * @param token The token.
     */
    public PersistentLogin(final User user, final String token) {
    	this.user = user;
    	this.token = token;
    }

	/**
	 * Returns id.
	 * @return the id.
	 */
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	/**
	 * Returns token.
	 * @return the token.
	 */
    @Column(nullable = false)
	public String getToken() {
		return token;
	}

	/**
	 * Returns user.
	 * @return the user.
	 */
	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "user_id")
	public User getUser() {
		return user;
	}

	/**
	 * Sets id.
	 * @param id the id to set.
	 */
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * Sets token.
	 * @param token the token to set.
	 */
	public void setToken(final String token) {
		this.token = token;
	}

	/**
	 * Sets user.
	 * @param user the user to set.
	 */
	public void setUser(final User user) {
		this.user = user;
	}

}
